#pragma once
#include <iostream>
#include <unordered_map>
#include <functional>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>



using task_t = std::function<void()>;

int nums=0;


void DownLoad()
{
    std::cout << "我是下载任务..., pid: " << getpid() << std::endl;
}

void Log()
{
    std::cout << "我是日志任务..., pid: " << getpid() <<std::endl;
}

void Sql()
{
    std::cout << "我是数据库同步任务..., pid: " << getpid() << std::endl;
}


class ptask
{
public:
    ptask()
    {
        Insert(Log);
        Insert(Sql);
        Insert(DownLoad);
    }

    void Insert(task_t func)
    {
        tasks[nums++]=func;
    }

    //选择任务
    int SeletTask()
    {
        return rand()%nums;
    }

    //如果有任务就执行任务
    void Excute(int num)
    {
        if(tasks.find(nums)==tasks.end())
            return 
        tasks[num]();
    }

private:
    std::unordered_map<int ,task_t> tasks;
};

ptask tes;

void Worker()
{
    while (true)
    {
        int cmd = 0;
        int n = ::read(0, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            tes.Excute(cmd);
        }
        else if (n == 0)
        {
            std::cout << "pid: " << getpid() << " quit..." << std::endl;
            break;
        }
        else
        {
        }
    }
}
